
package com.hmsg.health.dao;

import com.hmsg.health.bean.Dto.AppealDto;
import com.hmsg.health.bean.domain.AppealDo;
import com.hmsg.health.bean.domain.BannerDo;
import com.hmsg.health.bean.domain.TaskDo;
import com.hmsg.health.bean.domain.UserDo;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface AppealDao {
	
    @Select("select * from s_appeal where id=#{id}")
    AppealDo findOne(@Param("id")Integer id);
    
    
    @Select("<script>" +
    	    "select * from  `s_appeal` where 1=1 " +
    	    "    <if test='info.id != null'>" +
    	    "     and `id` = #{info.id} " +
    	    "    </if>" +
    	
    	    "    <if test='info.status != null'>" +
    	    "    and  `status` = #{info.status} " +
    	    "    </if>" +
    	    "    <if test='info.userId != null'>" +
    	    "    and  `user_id` = #{info.userId} " +
    	    "    </if>" +
    	    "    <if test='info.taskId != null'>" +
    	    "    and  `task_id` = #{info.taskId} " +
    	    "    </if>" +
    	    "</script>")
    List<AppealDo> select(@Param("info") AppealDo info);
  
  
    @Insert("<script>" +
    "insert into `s_appeal` " +
    "  <set>" +
    "    <if test='info.userTaskId != null'>" +
    "     `user_task_id` = #{info.userTaskId}, " +
    "    </if>" +
    "    <if test='info.status != null'>" +
    "      `status` = #{info.status}, " +
    "    </if>" +
    "    <if test='info.verifyImg != null'>" +
    "      `verify_img` = #{info.verifyImg}," +
    "    </if>" +
    "    <if test='info.createTime != null'>" +
    "      `create_time` = #{info.createTime}," +
    "    </if>" +
    "    <if test='info.taskId != null'>" +
    "      `task_id` = #{info.taskId}," +
    "    </if>" +
    "    <if test='info.appealDetail != null'>" +
    "      `appeal_detail` = #{info.appealDetail}," +
    "    </if>" +
    "    <if test='info.verifyDetail != null'>" +
    "      `verify_detail` = #{info.verifyDetail}," +
    "    </if>" +
    "    <if test='info.userId != null'>" +
    "      `user_id` = #{info.userId}" +
    "    </if>" +
    
    "  </set>" +
    "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int save(@Param("info") AppealDo info);


    @Update({"<script>",
		"UPDATE  s_appeal",
		"<trim prefix='set' suffixOverrides=','>",
		
		"<if test='user.status!=null'>",
		"status= #{user.status},",
		"</if>",
			"<if test='user.verifyReason!=null'>",
			"verify_reason= #{user.verifyReason}",
			"</if>",
		"</trim>",
		"where id = #{user.id}",
		"</script>"})
	@Options(useGeneratedKeys = true, keyProperty = "id")
 	Integer update(@Param("user") AppealDo user);


    //@Select("select * from s_appeal limit #{firstResult},#{maxResult}")
	@Select("<script>" +
			" select a.* from s_appeal  a  left join s_task s on a.task_id=s.id left join s_user u on a.user_id= u.id" +
			"   where 1=1  " +
			"    <if test='typeId != null '>" +
			"     and s.type=#{typeId} " +
			"    </if>" +
			"    <if test='phone != null '>" +
			"     and u.mobile=#{phone} " +
			"    </if>" +
			"    <if test='time != null '>" +
			"   and  DATE_FORMAT(a.create_time, '%Y-%m-%d') = DATE_FORMAT(#{time},'%Y-%m-%d') " +
			"    </if>" +
			"    order by a.id desc" +
			" limit #{firstResult},#{maxResult}"+
			"</script>")
    List<AppealDto> findAll(@Param("typeId") Integer typeId,
							@Param("phone") String phone,
							@Param("time") String time,
							@Param("firstResult") Integer firstResult,
							@Param("maxResult") Integer maxResult);


	@Select("<script>" +
			" select count(a.id) from s_appeal  a  left join s_task s on a.task_id=s.id left join s_user u on a.user_id= u.id" +
			"   where 1=1  " +
			"    <if test='typeId != null '>" +
			"     and s.type=#{typeId} " +
			"    </if>" +
			"    <if test='phone != null '>" +
			"     and u.mobile=#{phone} " +
			"    </if>" +
			"    <if test='time != null '>" +
			"   and  DATE_FORMAT(a.create_time, '%Y-%m-%d') = DATE_FORMAT(#{time},'%Y-%m-%d') " +
			"    </if>" +
			"    order by a.id desc" +
			"</script>")
    int findCount(@Param("typeId") Integer typeId,
				  @Param("phone") String phone,
				  @Param("time") String time);
}